package com.lipaluma.examples.annotation.custom;

import com.lipaluma.examples.annotation.custom.Invoice.Status;
import com.lipaluma.field.merge.TargetMergedAlgorithm;

public class TotalRemainingAddition implements TargetMergedAlgorithm {

	public void setParams(String... params) {}

	@Override
	public void initMergeOnFirstElement(Object target, String... params) {
		Client client = (Client)target;
		if(client.getInvoices().get(0).getStatus() == Status.STANDBY)
			client.setRemainingTotal(client.getTotalAmount());
	}

	@Override
	public Object merge(Object target, Object addon, String... params) {
		Client targetClient = (Client)target;
		Client clientToAdd = (Client)addon;
		
		if(clientToAdd.getInvoices().get(0).getStatus().equals(Status.STANDBY))
			targetClient.setRemainingTotal(targetClient.getRemainingTotal() + clientToAdd.getTotalAmount());
		return targetClient;
	}

	public Object[] unmerge(Object value) {
		return null;
	}

}
